package gd.hq.yolov5.caculate;

import android.util.Log;

import java.util.ArrayDeque;
import java.util.Queue;

import gd.hq.yolov5.App;
import gd.hq.yolov5.Box;

public class CalculateMotion {
    //相关数据
    public static Queue<PersonData> PERSON_DATES = new ArrayDeque<>();

    public static String calculateDirection(Box box,int width,int height){
        Log.d("direction","box："+box.x0+" "+box.x1+"  width:"+width);
        //拿到了数据，开始准备处理 ，先停下来
        //App.BLE_DEVICE.sendMsg("s");
        //先计算左右角度，以及人所占图像的宽度比比例，若比例大于80%则不处理
        float width_scale = (box.x1-box.x0)/width;
        if(width_scale<0.8){
            //计算图像中心距离人的x轴中心距离
            int center_x = (int) ((box.x1+box.x0)/2);
            //在右边
            if (center_x-width/2>(width/8)){
                Log.d("direction","right");
                return "4";
            }else if (center_x-width/2<-(width/8)){
                Log.d("direction","left");
                return "5";
            }
        }
        return null;
    }

    //计算
    public static String calculateDistance(Box box,int width,int height){
        Log.d("direction","box："+box.x0+" "+box.x1+"  width:"+width);
        //远的人是窄，近的人宽   从近到远，人会越来越窄，高度
        //算法1：面积占比
        float proportion = (box.x1-box.x0)*(box.y1-box.x0)/(width*height);
        Log.d("calc",proportion+"");
        if(proportion<0.15){
            return "f";
        }else {
            return "s";
        }
    }



    //在一个线程处理
    public static void startCalculate() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                Box box;
                int width,height;
                PersonData personData;
                //获取到数据
                while (true){
                    if(!PERSON_DATES.isEmpty()){
                        personData = PERSON_DATES.remove();
                        box = personData.getBox();
                        width = personData.getWidth();
                        height = personData.getHeight();
                        String result = calculateDistance(box,width,height);
                        String direction = calculateDirection(box,width,height);
                        if(App.BLE_DEVICE!=null){
                            App.BLE_DEVICE.sendMsg(result);
                        }
                        Log.d("calculate result:",result+"   "+direction);

                    }
                }
            }
        });
        thread.start();
    }
}
